//
//  YWAttendanceRecordViewController.m
//  maintain
//
//  Created by 郑想 on 16/2/23.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "UIColor+CustomColors.h"
#import "YWAttendanceRecordDetailTableViewController.h"
#import "YWAttendanceManager.h"
#import "YWAttendanceRecord.h"

#import "YWAttendanceRecordViewController.h"
#import "YWMacro.h"
#import "YWAttendanceRecordTableViewCell.h"
#import "UITableView+FDTemplateLayoutCell.h"

#define kRecordDicTitle @"title"
#define kRecordDicRecord @"records"

@interface YWAttendanceRecordViewController ()
@property (nonatomic, strong) NSArray* sourceDatas;
@end

@implementation YWAttendanceRecordViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = NSLocalizedString(@"attendanceRecord", nil);//@"签到记录";
    self.view.backgroundColor = [UIColor customBackgroudColor];

    [self.tableView registerClass:[YWAttendanceRecordTableViewCell class] forCellReuseIdentifier:YWAttendanceRecordTableViewCellIdentifier];
    
    self.tableView.separatorColor = [UIColor customLineColor];
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.estimatedRowHeight = 44.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

- (NSArray*)sourceDatas
{
    if (!_sourceDatas) {
        NSMutableArray* sourceDatas = [NSMutableArray array];
        NSArray* allRecords = [[YWAttendanceManager shareManager] attendanceRecordWithPage:0];
        for (YWAttendanceRecord* attendanceRecord in allRecords) {
            BOOL sameYearMonth = NO;
            for (NSMutableDictionary* showDic in sourceDatas) {
                if ([[showDic objectForKey:kRecordDicTitle] integerValue] == attendanceRecord.yearMonthDay) {
                    NSMutableArray* records = [NSMutableArray arrayWithArray:[showDic objectForKey:kRecordDicRecord]];
                    [records addObject:attendanceRecord];
                    [showDic setObject:records forKey:kRecordDicRecord];
                    sameYearMonth = YES;
                    break;
                }
            }
            if (!sameYearMonth) {
                NSMutableDictionary* showDic = [NSMutableDictionary dictionary];
                [showDic setObject:@(attendanceRecord.yearMonthDay) forKey:kRecordDicTitle];
                NSArray* array = [NSArray arrayWithObject:attendanceRecord];
                [showDic setObject:array forKey:kRecordDicRecord];
                [sourceDatas addObject:showDic];
            }
        }
        _sourceDatas = sourceDatas;
    }
    return _sourceDatas;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - UITableViewDataSource mothods
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return [tableView fd_heightForCellWithIdentifier:YWAttendanceRecordTableViewCellIdentifier cacheByIndexPath:indexPath configuration:^(YWAttendanceRecordTableViewCell *cell) {
//        cell.fd_enforceFrameLayout = NO;
//        NSDictionary* record = self.sourceDatas[indexPath.section];
//        NSArray* records = [record objectForKey:kRecordDicRecord];
//        cell.attendanceRecord = records[indexPath.row];
//    }];
//}

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
    return self.sourceDatas.count;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary* record = self.sourceDatas[section];
    NSArray* records = [record objectForKey:kRecordDicRecord];
    return records.count;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    NSDictionary* record = self.sourceDatas[indexPath.section];
    YWAttendanceRecordTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:YWAttendanceRecordTableViewCellIdentifier];
    cell.fd_enforceFrameLayout = NO;

    NSArray* records = [record objectForKey:kRecordDicRecord];
    cell.attendanceRecord = records[indexPath.row];
    return cell;
}

- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
    NSDictionary* record = self.sourceDatas[section];
    NSInteger yearMonth = [[record objectForKey:kRecordDicTitle] integerValue];
    NSString* title = [NSString stringWithFormat:@"%ld年%.2ld月", (long)(yearMonth / 100), (long)(yearMonth % 100)];
    return title;
}
#pragma mark - UITableViewDelegate mothods
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    YWAttendanceRecordDetailTableViewController* vc = [[YWAttendanceRecordDetailTableViewController alloc] init];
    NSDictionary* record = self.sourceDatas[indexPath.section];
    NSArray* records = [record objectForKey:kRecordDicRecord];
    vc.attendanceRecord = records[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
